package com.xhh.nphm.system.service.impl;

import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.utils.StringUtils;
import com.xhh.nphm.common.utils.sms.SmsUtils;
import com.xhh.nphm.system.domain.SmsNote;
import com.xhh.nphm.system.mapper.SmsNoteDao;
import com.xhh.nphm.system.service.ISmsNoteService;
import com.xhh.nphm.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SmsNoteServiceImpl implements ISmsNoteService {
    @Value("${sms.signMsg}")
    private String signMsg;

    @Value("${sms.sendBackMsg}")
    private String sendBackMsg;

    @Value("${sms.urgeTransact}")
    private String urgeTransact;

    @Value("${sms.enableSendSmsNote}")
    private boolean openSendSmsNote;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SmsNoteDao smsNoteDao;

    @Override
    public boolean sendSmsNote(String sbDeptId, String workFormNo, String type) {
        if(!openSendSmsNote){ // 配置是否开启短信通知
            return true;
        }
        boolean flag = false;
        if (!sbDeptId.contains(",")) {
            flag = sendNote(sbDeptId, workFormNo, type);
        } else {
            // 多个部门
            String[] arrDeptId = sbDeptId.split(",");
            for (int i = 0; i < arrDeptId.length; i++) {
                flag = sendNote(arrDeptId[i], workFormNo, type);
            }
        }
        return flag;
    }

    //发送
    private boolean sendNote(String sbDeptId, String workFormNo, String type){
        boolean flag = false;
        List<SysUser> sysUsers = sysUserService.selectUserByDeptId(sbDeptId);
        List<String> mobiles = new ArrayList<>();
        for (SysUser sysUser : sysUsers) {
            if (sysUserService.selectUserRoleId(sysUser.getUserId()) != null) {
                mobiles.add(sysUser.getPhonenumber());
            }
        }
        if (StringUtils.isNotEmpty(mobiles)) {
            for (String mobile : mobiles) {
                if (mobile != null && SmsUtils.isMobile(mobile)) {
                    //这是个接口
                    flag = SmsUtils.workFormNote(mobile, noteContent(type,workFormNo));
                }else{
                    System.err.println("手机号码不存在或不正确！");
                }
            }
        }
        return flag;
    }

    //配置短信模板
    private String noteContent(String type, String workFormNo){
        String content = new String();
        switch (type) {
            case "派单":
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
                content = String.format(signMsg, workFormNo, dateFormat.format(date).replaceAll(" ", ""));
                break;
            case "退回":
                content = String.format(sendBackMsg, workFormNo);
                break;
            case "催办":
                content = String.format(urgeTransact, workFormNo);
                break;
        }
        return content;
    }

    @Override
    public boolean updateState(SmsNote smsNote) {
        if(smsNote.getSmsInform() == null || smsNote.getSmsInform() == 0){
            smsNote.setSmsInform(0);//不开启
        }
        Integer smsNoteId = smsNoteDao.selectByDeptNo(smsNote); //判断是否有记录
        if(smsNoteId != null){
            smsNote.setId(smsNoteId);
            return smsNoteDao.updateState(smsNote);
        }
        return smsNoteDao.insertValue(smsNote) > 0;
    }

    /**
     * 电话退回
     * @param deptId
     * @param userId
     * @return
     */
    @Override
    public Integer selectSmsInform(String deptId, String userId) {
        return smsNoteDao.selectSmsInform(deptId,userId,1);
    }

    /**
     * 信件退回
     * @param deptId
     * @param userId
     * @return
     */
    @Override
    public Integer selectSmsInformMail(String deptId, String userId) {
        return smsNoteDao.selectSmsInform(deptId,userId,2);
    }
}
